import axios from 'axios'
import { payServer } from '@/plugins/http'

const APPID = 'wx32c0dfcb4dc3fc23' // FIXME

const payMixin = {
  data() {
    return {
      /**
       * 
       */
      zfdata: {}, // JSON.parse(cr('@ViewBag.JHPay')),
      /**
       * 
       */
      zf: {}, // JSON.parse(cr('@ViewBag.pay')),
    }
  },
  methods: {
    gotoPay(orderId) {
      // 微信code & openid
      // https://open.weixin.qq.com/connect/oauth2/authorize?
      //  appid=wx32c0dfcb4dc3fc23
      //  &redirect_uri=http://www.tssdeyy.com/Order/C_Home
      //  &response_type=code
      //  &scope=snsapi_base
      //  &state=123
      //  &connect_redirect=1
      //  #wechat_redirect
      
      /**
       * TODO:
       */
      // window.location.href = ``


      uni.showModal({
        title: '继续支付',
        content: '继续在线支付？（如需使用医保支付请取消，前往现场支付）',
        success: function (res) {
          if (res.confirm) {
            const redirect_uri = `${payServer}/static/wxpay.html`
            const appid = APPID
            window.location.href = 'https://open.weixin.qq.com/connect/oauth2/authorize?' +
              'appid=' + appid +
              '&redirect_uri=' + redirect_uri +
              '&response_type=code' +
              '&scope=snsapi_base' +
              '&state=' + orderId +
              '&connect_redirect=1' +
              '#wechat_redirect'

            // this.startPay()
          } else if (res.cancel) {
            uni.navigateTo({
              url: '/pages/hesuanyuyue/records/index'
            })
          }
        }
      })
    },

    /**
     * Load wx lib
     */
    initPay(cb) {
      const sc = document.createElement('script')
      sc.setAttribute('src', 'http://res.wx.qq.com/open/js/jweixin-1.4.0.js')
      sc.onload = cb || null
      document.head.appendChild(sc)
    },
    /**
     * Enter point
     */
    startPay() {
      if (typeof WeixinJSBridge == "undefined") {
        if (document.addEventListener) {
          document.addEventListener('WeixinJSBridgeReady', this.wxPay, false);
        } else if (document.attachEvent) {
          document.attachEvent('WeixinJSBridgeReady', this.wxPay);
          document.attachEvent('onWeixinJSBridgeReady', this.wxPay);
        }
      } else {
        this.wxPay();
      }
    },
    
    wxPay () {
      var wxdata = this.zfdata;
      WeixinJSBridge.invoke('getBrandWCPayRequest',
        {
          "appId": wxdata.appId,     //公众号名称，由商户传入
          "timeStamp": wxdata.timeStamp,         //时间戳，自1970年以来的秒数
          "nonceStr": wxdata.nonceStr, //随机串
          "package": wxdata.package,
          "signType": wxdata.signType,         //微信签名方式：
          "paySign": wxdata.paySign //微信签名
        },
        (res) => {
          console.log(res);

          if (res.err_msg == "get_brand_wcpay_request:ok") {
            uni.showLoading({
              title: '充值中请勿关闭页面',
            })

            /**
             * TODO:
             */
            axios.post("/Order/C_Home/PaySuccess", { ordernumber: vm.zf.ORDERID }).then(function (res) {
              console.log(res.data);

              if (res.data) {
                uni.hideLoading()
                uni.showModal({
                  title: '充值成功,1分钟内到账'
                })
              } else {
                uni.hideLoading()
                uni.showModal({
                  title: '充值失败'
                })
              }
            });
          } else {
            uni.hideLoading()
            uni.showModal({
              title: '支付失败'
            })
          }
        }
      );
    },

    /**
     * FIXME:
     */
    JHpayone () {
      var data = this.zfdata;
      //axios.default.headers.post['Content-Type'] = 'text/plain';
      axios.post("https://ibsbjstar.ccb.com.cn/CCBIS/ccbMain?CCB_IBSVersion=V6" +
        "&MERCHANTID=" + data.MERCHANTID + "&POSID=" + data.POSID + "&BRANCHID=" + data.BRANCHID +
        "&ORDERID=" + data.ORDERID + "&PAYMENT=" + data.PAYMENT + "&CURCODE=01&TXCODE=530590&REMARK1=" + data.REMARK1 +
        "&REMARK2=" + data.REMARK2 + "&TYPE=1&GATEWAY=0&CLIENTIP=" + data.CLIENTIP + "&REGINFO=" + escape(data.REGINFO) +
        "&PROINFO=" + data.PROINFO + "&REFERER=" + data.REFERER + "&TIMEOUT=" + data.TIMEOUT + "&TRADE_TYPE=" + data.TRADE_TYPE +
        "&SUB_APPID=" + data.SUB_APPID + "&SUB_OPENID=" + data.SUB_OPENID +
        "&MAC=" + data.MAC, "", { headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, }).then(function (res) {
          console.log(res.data);
          vm.JHpayTwo(res.data.ORDERURL);
        });
    },
    /**
     * FIXME:
     */
     JHpayTwo (url) {
      axios.post(url).then(function (res) {
        console.log(res.data);
        vm.wxPay(res.data)
      });
    },
    /**
     * FIXME:
     */
    JHpayThree () {
      var data = this.zfdata;
      $.ajax({
        async: false,
        crossDomain: true,
        url: "https://ibsbjstar.ccb.com.cn/CCBIS/ccbMain?CCB_IBSVersion=V6" +
        "&MERCHANTID=" + data.MERCHANTID + "&POSID=" + data.POSID + "&BRANCHID=" + data.BRANCHID +
        "&ORDERID=" + data.ORDERID + "&PAYMENT=" + data.PAYMENT + "&CURCODE=01&TXCODE=530590&REMARK1=" + data.REMARK1 +
        "&REMARK2=" + data.REMARK2 + "&TYPE=1&GATEWAY=0&CLIENTIP=" + data.CLIENTIP + "&REGINFO=" + escape(data.REGINFO) +
        "&PROINFO=" + data.PROINFO + "&REFERER=" + data.REFERER + "&TIMEOUT=" + data.TIMEOUT + "&TRADE_TYPE=" + data.TRADE_TYPE +
        "&SUB_APPID=" + data.SUB_APPID + "&SUB_OPENID=" + data.SUB_OPENID +
        "&MAC=" + data.MAC,
        method: "POST",
        dataType: "json",
        //headers: {
        // "content-type": "application/x-www-form-urlencoded",
        // "cache-control": "no-cache"
        //},
        //data: {
        //    "authMode": "cert",
        //    "original": original,
        //    "signed_data": signResult
        //},
        success: function (response) {
            console.log(response);
        }
      })
      var data = this.zfdata;
    },
  }
}

export default payMixin